/*
 * @lc app=leetcode.cn id=1356 lang=javascript
 *
 * [1356] 根据数字二进制下 1 的数目排序
 */

// @lc code=start
const number2 = function (num) {
  let sum = 0;
  while (num) {
    num = num & (num - 1);
    sum++;
  }
  return sum;
}
/**
 * @param {number[]} arr
 * @return {number[]}
 */
var sortByBits = function(arr) {
  return arr.sort((a, b) => {
    if(number2(a) !== number2(b)) {
      return number2(a) - number2(b);
    } else {
      return a - b;
    }
  })
};
// @lc code=end

